//
//  MyViewController.m
//  月色:(我)
//
//  Created by 殷殷明静 on 16/9/7.
//  Copyright © 2016年 MJ.Y. All rights reserved.
//
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#import "MyViewController.h"
#import "LoginAndRegisterViewController.h"
#import "MyCollectionViewController.h"
#import "SuggestionViewController.h"
#import "SDImageCache.h"
#import "SetViewController.h"
@interface MyViewController ()<SetViewControllerDelegate>
@property (nonatomic, strong) UIButton *loginOrRegisterBtn;
@property (nonatomic, strong) UIView *redLine;
@property (nonatomic, strong) UIButton *myCollectionBtn;
@property (nonatomic, strong) UIButton *myCleanBtn;
@property (nonatomic, strong) UIButton *mySuggestBtn;
@property (nonatomic, strong) UIButton *mySetBtn;
@property (nonatomic, strong) UIView *myView;
@property (nonatomic, strong) UIImageView *personImageView;
@property (nonatomic, strong) UIImage *defaultImage;
@property (nonatomic, strong) UILabel *label;
@end
// 320 568
@implementation MyViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _isLogined = NO;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = 0;
    // 创建"红色分割线"
    [self createRedLineView];
    // 我的收藏
    [self reateMyCellectionButton];
    // 清除缓存
    [self createMycleanButton];
    // 意见反馈
    [self createSuggestionButton];
    // 设置
    [self createSetButton];
}
- (void)createSetButton{
    _mySetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:_mySetBtn];
    _mySetBtn.frame = CGRectMake(0, 0, 80, 30);
    _mySetBtn.center = CGPointMake(WIDTH / 2, HEIGHT / 2 + 90 -64);
    [_mySetBtn setTitleColor:[UIColor blackColor] forState:0];
    [_mySetBtn setTitle:@"设置" forState:0];
    _mySetBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [_mySetBtn addTarget:self action:@selector(pushToSetVC:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)createSuggestionButton {
    _mySetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:_mySetBtn];
    _mySetBtn.frame = CGRectMake(0, 0, 80, 30);
    _mySetBtn.center = CGPointMake(WIDTH / 2, HEIGHT / 2 + 45 -64);
    [_mySetBtn setTitleColor:[UIColor blackColor] forState:0];
    [_mySetBtn setTitle:@"意见反馈" forState:0];
    _mySetBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [_mySetBtn addTarget:self action:@selector(pushToSuggestionVC:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)createMycleanButton {
    _mySetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:_mySetBtn];
    _mySetBtn.frame = CGRectMake(0, 0, 80, 30);
    _mySetBtn.center = CGPointMake(WIDTH / 2, HEIGHT / 2 -64);
    [_mySetBtn setTitleColor:[UIColor blackColor] forState:0];
    [_mySetBtn setTitle:@"清除缓存" forState:0];
    _mySetBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [_mySetBtn addTarget:self action:@selector(cleanCache:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)reateMyCellectionButton {
    _mySetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:_mySetBtn];
    _mySetBtn.frame = CGRectMake(0, 0, 80, 30);
    _mySetBtn.center = CGPointMake(WIDTH / 2, 240 - 64);
    [_mySetBtn setTitleColor:[UIColor blackColor] forState:0];
    [_mySetBtn setTitle:@"我的收藏" forState:0];
    _mySetBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [_mySetBtn addTarget:self action:@selector(pushToMyCollectionVC:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)createRedLineView {
    _redLine = [[UIView alloc] init];
    [self.view addSubview:_redLine];
    _redLine.frame = CGRectMake(0, 0, 30, 1);
    _redLine.center = CGPointMake(WIDTH / 2, 200 - 64);
    _redLine.backgroundColor = [UIColor redColor];
}

// 懒加载注册登录按钮
- (UIButton *)loginOrRegisterBtn {
    if (!_loginOrRegisterBtn) {
        _loginOrRegisterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.view addSubview:_loginOrRegisterBtn];
        _loginOrRegisterBtn.frame = CGRectMake(0, 0, 80, 30);
        _loginOrRegisterBtn.center = CGPointMake(WIDTH / 2, 110 - 64);
        // 渲染
        _loginOrRegisterBtn.backgroundColor = [UIColor clearColor];
        [_loginOrRegisterBtn setTitle:@"登录/注册" forState:0];
        _loginOrRegisterBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [_loginOrRegisterBtn setTitleColor:[UIColor grayColor] forState:0];
        _loginOrRegisterBtn.layer.borderWidth = 1;
        _loginOrRegisterBtn.layer.cornerRadius = 5;
        _loginOrRegisterBtn.layer.borderColor = [UIColor blackColor].CGColor;
        // 点击事件-跳转页面
        [_loginOrRegisterBtn addTarget:self action:@selector(goLoginAndRegisterPage) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginOrRegisterBtn;
}
// 登录和注册的跳转
- (void)goLoginAndRegisterPage {
    [self setHidesBottomBarWhenPushed:YES];
    LoginAndRegisterViewController *loginAndRegisterVC = [LoginAndRegisterViewController new];
    [self setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:loginAndRegisterVC animated:YES];
    [self setHidesBottomBarWhenPushed:NO];
}
// 设置透明navigationBar
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)createPersonImageView {
    _myView = [[UIView alloc] init];
    [self.view addSubview:_myView];
    _myView.frame = CGRectMake(0, 0, 80, 120);
    _myView.center = CGPointMake(WIDTH / 2, 110 -64);
    _myView.backgroundColor = [UIColor clearColor];
//     我的原始图片
    UIImage *defaultImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_portrait]]];

    UIImageView *personImageView = [[UIImageView alloc] initWithImage:defaultImage];
    personImageView.frame = CGRectMake(- 5, 0, _myView.frame.size.width + 30, _myView.frame.size.height - 22);
    [_myView addSubview:personImageView];
//     我的个人账号
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(personImageView.frame.origin.x + 6, personImageView.frame.origin.y + personImageView.frame.size.height + 10, _myView.frame.size.width+ 30, 22);
    label.font = [UIFont systemFontOfSize:12];
    [_myView addSubview:label];
    label.text = _nickName;
}

- (void)fileSize{
    
    NSFileManager *manager = [NSFileManager defaultManager];
    
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    
    NSArray *files = [manager subpathsOfDirectoryAtPath:cachePath error:nil]; // 递归所有子路径
    
    NSInteger totalSize = 0;
    
    for (NSString *filePath in files) {
        NSString *path = [cachePath stringByAppendingPathComponent:filePath];
        // 判断是否为文件
        BOOL isDir = NO;
        [manager fileExistsAtPath:path isDirectory:&isDir];
        if (!isDir) {
            NSDictionary *attrs = [manager attributesOfItemAtPath:path error:nil];
            totalSize += [attrs[NSFileSize] integerValue];
        }
    }
    
            NSString *sizeStr = [NSString stringWithFormat:@"%.2f M",totalSize / 1000.0 / 1000.0];
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"清除缓存" message:sizeStr preferredStyle:UIAlertControllerStyleAlert];
                [alertController addAction:[UIAlertAction actionWithTitle:@"确定"  style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    
    
                }]];
                [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    
                }]];
    
                //弹出alert
                [self presentViewController:alertController animated:YES completion:nil];
    
                
    

}

- (void)cleanCache:(UIButton *)btn {
//    double size = [[SDImageCache sharedImageCache] getSize] / 1000.0 / 1000.0;
    [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
        // 清除完毕的处理。
        [self fileSize];
    }];
    

}
// push 我的收藏
- (void)pushToMyCollectionVC:(UIButton *)btn {
    MyCollectionViewController *collect = [MyCollectionViewController new];
    [self setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:collect animated:YES];
    [self setHidesBottomBarWhenPushed:NO];
}
// push 意见反馈
- (void)pushToSuggestionVC:(UIButton *)btn {
    SuggestionViewController *suggestionVC = [SuggestionViewController new];
    [self setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:suggestionVC animated:YES];
    [self setHidesBottomBarWhenPushed:NO];
}
// push 我的设置
- (void)pushToSetVC:(UIButton *)btn {

    SetViewController *setVC = [SetViewController new];
    setVC.delegate = self;
    setVC.isLogin = _isLogined;
    [self setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:setVC animated:YES];
    [self setHidesBottomBarWhenPushed:NO];
    
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    // 创建个人图片
    [self createPersonImageView];
    //
    if (_isLogined) {
        _myView.hidden = NO;
        self.loginOrRegisterBtn.hidden = YES;
    } else {
        _myView.hidden = YES;
        self.loginOrRegisterBtn.hidden = NO;
    }
    
    
}
- (void)exitLogin:(BOOL)isLogin {
    _isLogined = isLogin;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [_myView removeFromSuperview];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
